package com.shadt.core.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class XMLUtil {
	/**
	 * 流转二进制数组
	 * 
	 * @param in
	 * @return
	 * @throws IOException
	 */
	public static byte[] readInput(InputStream in) throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int len = 0;
		byte[] buffer = new byte[1024];
		while ((len = in.read(buffer)) > 0) {
			out.write(buffer, 0, len);
		}
		out.close();
		in.close();
		return out.toByteArray();
	}

	/**
	 * 流转字符串
	 * 
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static String inputStreamToString(InputStream is) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		 
		int i;
		while ((i = is.read()) != -1) {
			baos.write(i);
		}
		return baos.toString("utf-8");
	}

	/**
	 * 字符串转流
	 * 
	 * @param sInputString
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static InputStream getStringStream(String sInputString) throws UnsupportedEncodingException {
		ByteArrayInputStream tInputStringStream = null;
		if (sInputString != null && !sInputString.trim().equals("")) {
			tInputStringStream = new ByteArrayInputStream(sInputString.getBytes("utf-8"));
		}
		return tInputStringStream;
	}


	
	/********************************************************************************************/
	/**
	 * @param map
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static String getStringFromMap(Map<String, Object> map, String key, String defaultValue) {
		if (key == "" || key == null) {
			return defaultValue;
		}
		String result = (String) map.get(key);
		if (result == null) {
			return defaultValue;
		} else {
			return result;
		}
	}

	/**
	 * @param map
	 * @param key
	 * @return
	 */
	public static int getIntFromMap(Map<String, Object> map, String key) {
		if (key == "" || key == null) {
			return 0;
		}
		if (map.get(key) == null) {
			return 0;
		}
		return Integer.parseInt((String) map.get(key));
	}

 

 

	/**
	 * 是否支持微信支付
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isSupportWXPay(HttpServletRequest request) {
		System.out.println("tou:" + request.getHeader("User-Agent"));
		String ua = request.getHeader("User-Agent");
		int pos = ua.indexOf("MicroMessenger");
		if (pos <= 0) {
			return false;
		}
		char ver = ua.charAt(pos + 15);
		if (ver < '5') {
			return false;
		}
		return true;
	}

	/**
	 * @return
	 */
	public static String getTimeStamp() {
		return String.valueOf(new Date().getTime() / 1000);
	}

	
	
	
	
}
